package com.ph.order.api.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ph.base.BaseController;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.permission.vo.SessionUserVo;
import com.ph.shopping.facade.product.service.IProductService;
import com.ph.shopping.facade.product.vo.PhProductVo;

import cm.ph.shopping.facade.order.entity.PhMerchantOrder;
import cm.ph.shopping.facade.order.service.IMerchantOrderService;
import cm.ph.shopping.facade.order.service.IPhManagerOrderAddressService;
import cm.ph.shopping.facade.order.vo.PhLogisticsVo;
import cm.ph.shopping.facade.order.vo.PhManagerOrderAddressVo;
import cm.ph.shopping.facade.order.vo.PhMerchantOrderProductsVo;
import cm.ph.shopping.facade.order.vo.PhProductForOderUseVo;

/**
 * 
 * @项目：phshopping-facade-
 *
 * @描述：商户发货、取消订单控制层
 *
 * 					@作者： yyg
 *
 * @创建时间：2017年3月28日 下午1:42:47
 *
 * @Copyright by yyg
 */
@Controller
@RequestMapping("api/forOrder")
public class ChargeBackController extends BaseController {

	@Reference(version = "1.0.0")
	private IPhManagerOrderAddressService iPhManagerOrderAddressService;

	@Reference(version = "1.0.0")
	private IProductService iProductService;

	@Reference(version = "1.0.0")
	private IMerchantOrderService iMerchantOrderService;

	/**
	 * 取消订单
	 * 
	 * @param orderId
	 * @param request
	 * @return
	 *
	 */
	@RequestMapping("/chargeBack")
	@ResponseBody
	public Result chargeBack(Long orderId, HttpServletRequest request) {
		// 获取session，判断是商户取消订单还是代理商
		SessionUserVo info = getLoginUser(request);
		try {
			// 查询订单下的商品
			PhMerchantOrderProductsVo orderProductsVo = new PhMerchantOrderProductsVo();
			orderProductsVo.setMerchantOrderId(orderId);
			List<PhMerchantOrderProductsVo> list = this.iPhManagerOrderAddressService
					.findOrderProducts(orderProductsVo);
			// 商品集合
			List<PhProductForOderUseVo> productVoList = new ArrayList<PhProductForOderUseVo>();
			// 查询订单下的商品对应商品表数据 进行商品数量 计算操作
			for (PhMerchantOrderProductsVo phMerchantOrderProductsVo : list) {
				PhProductVo phProductVoForQuery = new PhProductVo();
				phProductVoForQuery.setId(phMerchantOrderProductsVo.getProductId());
				PhProductVo phProductVo = this.iProductService.getPhProductVo(phProductVoForQuery);
				// 当前退单商品数量加上现有商品数量 为商品数量
				phProductVo.setProductCount(phMerchantOrderProductsVo.getNum() + phProductVo.getProductCount());

				PhProductForOderUseVo forOderUseVo = new PhProductForOderUseVo();
				forOderUseVo.setId(phProductVo.getId());
				forOderUseVo.setProductCount(phProductVo.getProductCount());
				productVoList.add(forOderUseVo);
			}

			PhMerchantOrder order = new PhMerchantOrder();
			order.setId(orderId);
			order.setCancelUserId(info.getId());
			order.setUpdateTime(new Date());
			order.setStatus(5);
			this.iPhManagerOrderAddressService.orderProductBack(order, productVoList);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("id为" + info.getId() + "的用户取消订单(订单ID" + orderId + ")出错！" + e.getMessage());
			return getResult(ResultEnum.CANCEL_ORDER_DEFAULT_FAIL);
		}
		return getResult(ResultEnum.SUCCESS);
	}

	/**
	 * 获取未删除的物流信息（商户代理商通用）
	 * 
	 * @author yyg
	 */
	@RequestMapping("/getLogistics")
	@ResponseBody
	public Result getLogisticsInformation() {
		// 获取session
		Result result = new Result();
		PhLogisticsVo logisticsVo = new PhLogisticsVo();
		List<PhLogisticsVo> list = this.iPhManagerOrderAddressService.findLogistics(logisticsVo);
		result.setData(list);
		return result;
	}

	/**
	 * 获取当前用户的是发货地址 （商户代理商通用）
	 * 
	 * @author yyg
	 */
	@RequestMapping("/shippingAddress")
	@ResponseBody
	public Result getSssionUserShippingAddress(HttpServletRequest request) {
		// 获取session
		SessionUserVo info = getLoginUser(request);
		Result result = new Result();
		PhManagerOrderAddressVo addressVo = new PhManagerOrderAddressVo();
		addressVo.setUserId(info.getId());
		addressVo.setIsable(1);// 已经启用的地址
		List<PhManagerOrderAddressVo> list = this.iPhManagerOrderAddressService.findAddress(addressVo);
		result.setData(list);
		return result;
	}

	/***
	 * 发货方法
	 * 
	 * @param request
	 * @return
	 * @author yyg
	 */
	@RequestMapping("/Shipping")
	@ResponseBody
	public Result Shipping(@ModelAttribute PhMerchantOrder phMerchantOrder, HttpServletRequest request) {
		// 获取session
		SessionUserVo info = getLoginUser(request);
		//先查询发货详细地址和联系人以及联系人电话
		PhManagerOrderAddressVo addressVo = new PhManagerOrderAddressVo();
		addressVo.setUserId(info.getId());
//		addressVo.setIsable(1);// 已经启用的地址
		addressVo.setId(phMerchantOrder.getSendAddressId());
		PhManagerOrderAddressVo addressVoQuery = this.iPhManagerOrderAddressService.findAddress(addressVo).get(0);
		//给phMerchantOrder赋值
//		phMerchantOrder.setSendAddress(addressVoQuery.getProvinceName()+addressVoQuery.getCityName()+addressVoQuery.getAreaName()+addressVoQuery.getTownName()+addressVoQuery.getAddress());
		phMerchantOrder.setSendAddress(addressVoQuery.getProvinceName()+addressVoQuery.getCityName()+addressVoQuery.getAreaName()+addressVoQuery.getAddress());
		phMerchantOrder.setSendTelPhone(addressVoQuery.getTelPhone());
		phMerchantOrder.setSendContacts(addressVoQuery.getContacts());
		
		phMerchantOrder.setUpdateTime(new Date());
		phMerchantOrder.setStatus(3);
		try {
			this.iMerchantOrderService.updateForShipping(phMerchantOrder);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("id为" + info.getId() + "的用户发货(订单ID" + phMerchantOrder.getId() + ")出错！" + e.getMessage());
			return getResult(ResultEnum.SHIPPING_DEFAULT_FAIL);
		}
		return getResult(ResultEnum.SUCCESS);
	}

}
